<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="600px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="108px" :rules="formRules">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入分类名称" />
                </el-form-item>
                <el-form-item label="父级分类" prop="pid">
                    <el-select v-model="formData.pid" clearable placeholder="请选择父级分类">
                        <el-option v-for="item in pidList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" clearable placeholder="请输入排序" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="roomEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { sacrificeCateAdd, sacrificeCateEdit, sacrificeCateDetail, sacrificeCateLists } from '@/api/sacrificeManage'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { ref, computed, onMounted, reactive, shallowRef } from 'vue'

// 定义配置项接口
interface ConfigItem {
    key: string;
    value: string;
}

// 定义配置对象接口
interface ConfigObject {
    [key: string]: string;
}

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑分类' : '新增分类'
})

const hotelList = ref<any[]>([])

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    sort: 0,
    pid: '' as number | string
})

// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入分类名称',
        trigger: ['blur']
    }],
})

const pidList = ref<any[]>([])

// 在组件挂载时初始化配置项
onMounted(() => {
    getPidList()
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    formData.id = data.id
}

// 获取父级分类
const getPidList = async () => {
    const data = await sacrificeCateLists({page_on: 1, page_size: 1000})
    pidList.value = data.lists
}

const getDetail = async (row: Record<string, any>) => {
    const data = await sacrificeCateDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' 
        ? await sacrificeCateEdit(data) 
        : await sacrificeCateAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>

<style scoped>
.config-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.config-item {
    display: flex;
    gap: 10px;
    align-items: center;
}

.config-key-input {
    width: 200px;
}

.config-value-input {
    flex: 1;
}
</style>
